package com.xinya.taobaosale.ui.adapter;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xinya.taobaosale.R;
import com.xinya.taobaosale.model.domain.HomePagerDiscoveryBean;

import org.jetbrains.annotations.NotNull;

/**
 * @ClassName: HomePageListAdapter$
 * @Description: java类作用描述
 * @Author: xinya
 * @CreateDate: 2021/2/3$ 19:34$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class HomePageListAdapter extends BaseQuickAdapter<HomePagerDiscoveryBean, BaseViewHolder> implements LoadMoreModule {
    private static final String TAG = "HomePageListAdapter";

    public HomePageListAdapter(int layoutResId) {
        super(layoutResId);
    }

    @SuppressLint("DefaultLocale")
    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder,
                           HomePagerDiscoveryBean bean) {
        // title
        baseViewHolder.setText(R.id.goods_title, bean.getTitle());
        // 打折后价格
        baseViewHolder.setText(R.id.off_prise, String.format("%.2f", Float.parseFloat(bean.getZk_final_price()) - bean.getCoupon_amount()) + "元");
        // 可以节省多少钱
        baseViewHolder.setText(R.id.off_amount, "省" + bean.getCoupon_amount() + "元");
        // 原价
        baseViewHolder.setText(R.id.origin_prise, bean.getZk_final_price() + "元");
        TextView originPrice = baseViewHolder.getView(R.id.origin_prise);
        // 设置原价的中横线
        originPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        // 设置销量
        int volume = bean.getVolume();
        String volumeString = String.valueOf(volume);
        if (volume >= 10000) {
            volumeString = String.format("销量%.2f万", volume * 1f / 10000);
        }
        baseViewHolder.setText(R.id.bought_count, volumeString);
        // 获取 && 设置指定大小的图片
        ImageView goodsCover = baseViewHolder.findView(R.id.goods_cover);
        int size = goodsCover.getLayoutParams().width / 2;
        String url = "https:" + bean.getPict_url() + "_" + size + "x" + size +".jpg";
        Glide.with(getContext()).load(url).into(goodsCover);

        Log.d(TAG, "convert: " + bean.getTitle());

    }
}
